﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.Common.Script;

public partial class Theme_3_SelectCourse :PowerCreator.LMS.Web.Base.BasePage
{
    PowerCreator.LMS.Web.Catalog.Catalog opCatalog = new PowerCreator.LMS.Web.Catalog.Catalog();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Web.Course.UserCourse opUserCourse = new PowerCreator.LMS.Web.Course.UserCourse();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "UserLogin.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        this.Title = config.GetLanagerValueByKey("theme_Study_SelectCourse", PowerCreator.LMS.Language.LanagetItemType.Page);
        //this.Title += "----" + config.GetLanagerValueByKey("theme_Study_SelectCourse", PowerCreator.LMS.Language.LanagetItemType.Page);
        //if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        //{
        //    PowerCreator.LMS.Common.Script.Script.ShowAndCloseMe(this, config.GetLanagerValueByKey("theme_Channel_LoginMsg", PowerCreator.LMS.Language.LanagetItemType.Page));
        //    return;
        //}
        if (!IsPostBack)
        {
            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    protected override void InitPage()
    {
        base.InitPage();
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }

    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();

        sbSearch.Append(" 1=1 ");
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] != null)
        {
            PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            sbSearch.Append(string.Format("and Course_Id not in (select FKUCCourseID from UserCourse where UCUserID={0} and UCState=1)  ", login.UserID));
        }
        sbSearch.Append("and  CourseState = 1 and CourseAllowSelect=1 and CourseIsCompulsory<>3");

        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = PowerCreator.LMS.Web.Config.WebConfig.PageSize;
        AspNetPager1.RecordCount = opCourse.QueryCourseCount(search);
    }
    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = opCourse.QueryCourse(search,PowerCreator.LMS.Web.Config.WebConfig.PageSize,pageIndex);
        rpt_Course.DataSource = dt;
        rpt_Course.DataBind();

        //ViewState["MyCoueseIDlist"] = ",";
        ViewState["MyOldCoueseIDlist"] = ",";

        for (int i = 0; i < rpt_Course.Items.Count; i++)
        {
            Label lbl_CourseID = (Label)rpt_Course.Items[i].FindControl("lbl_CourseID");
            Label lbl_msg = (Label)rpt_Course.Items[i].FindControl("lbl_msg");
            LinkButton lnkbtn_Select = (LinkButton)rpt_Course.Items[i].FindControl("lnkbtn_Select");
            PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            string sql = "UCUserID=" + login.UserID + " and FKUCCourseID =" + lbl_CourseID.Text;
            if (opUserCourse.QueryUserCourse(sql) >= 1)
            {
                //lbl_msg.Text = config.GetLanagerValueByKey("State_Msg3", PowerCreator.LMS.Language.LanagetItemType.Page);
                //lnkbtn_Select.Visible=false;
                //ViewState["MyCoueseIDlist"] += lbl_CourseID.Text+",";
                ViewState["MyOldCoueseIDlist"] += lbl_CourseID.Text + ",";
                CheckBox cbox = (CheckBox)rpt_Course.Items[i].FindControl("chkSelect");
                cbox.Checked = true;
            }
        }
    }

    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        chkCancel2.Checked = false;
        chkAll2.Checked = chkAll.Checked;
        //全选
        for (int i = 0; i < rpt_Course.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_Course.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        chkAll2.Checked = false;
        chkCancel2.Checked = chkCancel.Checked;
        //返回
        for (int i = 0; i < rpt_Course.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_Course.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }


    protected void chkAll_CheckedChanged2(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        chkCancel2.Checked = false;
        chkAll.Checked = chkAll2.Checked;
        //全选
        for (int i = 0; i < rpt_Course.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_Course.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged2(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        chkAll2.Checked = false;
        chkCancel.Checked = chkCancel2.Checked;
        //返回
        for (int i = 0; i < rpt_Course.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_Course.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }



    protected void btn_SubmibClick(object sender, EventArgs e)
    {
        ViewState["MyCoueseIDlist"] = ",";
        for (int i = 0; i < rpt_Course.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_Course.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
            {

                Label lblid = (Label)rpt_Course.Items[i].FindControl("lblid");
                ViewState["MyCoueseIDlist"] += lblid.Text + ",";
                ViewState["MyOldCoueseIDlist"] = ViewState["MyOldCoueseIDlist"].ToString().Replace("," + lblid.Text + ",", ",");

                string search = string.Format(" UCUserID={0} and FKUCCourseID = {1} ", login.UserID, lblid.Text);
                if (opUserCourse.QueryUserCourse(search) == 0)
                {
                    PowerCreator.LMS.Course.Entity.UserCourse voUserCourse = new PowerCreator.LMS.Course.Entity.UserCourse();
                    voUserCourse.FKUCCourseID = int.Parse(lblid.Text);
                    voUserCourse.UCState = 0;
                    voUserCourse.UCUserID = login.UserID;
                    opUserCourse.AddUserCourse(voUserCourse);
                }



            }
        }
        string oldidlist = ViewState["MyOldCoueseIDlist"].ToString();
        if (oldidlist != "," & oldidlist.Length > 1)
        {
            int len = oldidlist.Length;
            oldidlist = oldidlist.Substring(1, len - 2);
            if (oldidlist.Length > 0)
            {
                string[] delIDs = oldidlist.Split(',');
                for (int j = 0; j < delIDs.Length; j++)
                {
                    opUserCourse.DelUserCourse(int.Parse(delIDs[j]), login.UserID);
                }

            }
        }

        ViewState["MyOldCoueseIDlist"] = ViewState["MyCoueseIDlist"];
        string sql = GetSearch();
        BinderPager(sql);
        BinderRPT(sql, 1);
    }

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }

}
